<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Group4_UniHub</title>
    <style>
        html {
        font-family: "Times New Roman", sans-serif;
        background-color: rgb(251,248,239,0.6);
        }
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .container {
            display: flex;
            align-items: center;
            background-color: rgb(255, 255, 255);
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
            padding: 50px;
            border-radius: 10px;
        }
        .form-container {
            display: flex;
            flex-direction: column;
            align-items: left;
        }
        form {
            display: none;
            flex-direction: column;
            align-items: center;
            width: 100%;
        }
        form.active {
            display: flex;
        }
        label {
            margin-top: 10px;
        }
        input {
            margin-top: 5px;
            padding: 10px;
            width: 100%;
            max-width: 300px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        button {
            margin: 25px 0 15px 0;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            background-color: rgb(0,120,212,0.8);
            color: rgb(255, 255, 255);
            cursor: pointer;
            box-shadow: 5px 5px 10px rgba(0,0,0,0.1);
            width: 200px;
        }
        button:hover {
            background-color: rgb(0,120,212);
        }
        p {
            margin-top: 10px;
        }
        a {
            color: rgb(0,120,212);
            text-decoration: none;
        }
        a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
  <div class="container">
    <div class="form-container">
        <!-- Registration Form -->
        <form id="registerForm">
            <h1>Register</h1>
            <label for="registerUsername">Username:</label>
            <input type="text" id="registerUsername" name="username" required>
            <label for="registerEmail">Email:</label>
            <input type="email" id="registerEmail" name="email" required>
            <label for="registerPassword">Password:</label>
            <input type="password" id="registerPassword" name="password" required>
            <button type="submit">Register</button>
            <p>Already have an account? <a href="#" class="switch-link" data-target="loginForm">Login here</a></p>
        </form>

        <!-- Login Form -->
        <form id="loginForm" class="active">
            <h1>Login</h1>
            <label for="loginEmail">Email:</label>
            <input type="email" id="loginEmail" name="email" required>
            <label for="loginPassword">Password:</label>
            <input type="password" id="loginPassword" name="password" required>
            <button type="submit">Login</button>
            <p>Don't have an account? <a href="#" class="switch-link" data-target="registerForm">Register here</a></p>
        </form>
    </div>
    <img src="images/logo.png" alt="logo" style="width: 45vh; height: 45vh;">
  </div>

  <script>
    document.addEventListener('DOMContentLoaded', function() {
        const forms = document.querySelectorAll('form');
        const switchLinks = document.querySelectorAll('.switch-link');

        // Function to switch forms
        function switchForm(targetFormId) {
            forms.forEach(form => {
                form.classList.remove('active');
                if (form.id === targetFormId) {
                    form.classList.add('active');
                }
            });
        }

        // Event listener for switching forms
        switchLinks.forEach(link => {
            link.addEventListener('click', function(event) {
                event.preventDefault();
                const targetFormId = this.getAttribute('data-target');
                switchForm(targetFormId);
            });
        });

        // Registration form submit event
        document.getElementById('registerForm').addEventListener('submit', async function(event) {
            event.preventDefault();
            const formData = {
                username: registerForm.username.value,
                email: registerForm.email.value,
                password: registerForm.password.value
            };

            try {
                const response = await fetch('/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(formData)
                });

                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }

                const data = await response.json();
                alert(data.message);
                switchForm('loginForm');
            } catch (error) {
                console.error('There was a problem with your fetch operation:', error);
                alert('Registration failed. Please try again.');
            }
        });

        // Login form submit event
        document.getElementById('loginForm').addEventListener('submit', async function(event) {
            event.preventDefault();
            const loginData = {
                email: loginForm.email.value,
                password: loginForm.password.value
            };

            try {
                const response = await fetch('/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(loginData)
                });

                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }

                const data = await response.json();
                console.log('Response data:', data);
                localStorage.setItem('token', data.token);
                localStorage.setItem('userId', data.userId); //Ensure that the userId is stored here
                alert(data.message);

                //Redirects to/student page
                window.location.href = '/student';
            } catch (error) {
                console.error('There was a problem with your fetch operation:', error);
                alert('Login failed. Please check your credentials and try again.');
            }
        });
    });
  </script>
</body>
</html>
